package com.zhouwr.system.vo;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.zhouwr.common.core.util.StringUtils;
import com.zhouwr.common.core.vo.TreeNode;
import com.zhouwr.system.entity.PermissionEntity;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;

/**
 * 路由配置信息
 *
 * @author ruoyi
 */
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@Data
public class RouterTreeVo extends TreeNode<RouterTreeVo> {
    /**
     * 路由id
     */
    private String id;

    /**
     * 父路由id
     */
    private String parentId;

    /**
     * 路由名字
     */
    private String name;

    /**
     * 路由地址
     */
    private String path;

    /**
     * 是否隐藏路由，当设置 true 的时候该路由不会再侧边栏出现
     */
    private boolean hidden;

    /**
     * 重定向地址，当设置 noRedirect 的时候该路由在面包屑导航中不可被点击
     */
    private String redirect;

    /**
     * 组件地址
     */
    private String component;

    /**
     * 当你一个路由下面的 children 声明的路由大于1个时，自动会变成嵌套的模式--如组件页面
     */
    private Boolean alwaysShow;

    /**
     * 其他元素
     */
    private MetaVo meta = new MetaVo();
    /**
     * 子路由
     */
    private List<RouterTreeVo> children = new ArrayList<>();

    public RouterTreeVo(PermissionEntity permission) {
        this.id = permission.getId();
        this.name = permission.getName();
        this.path = permission.getPath();
        this.component = permission.getComponent();
        this.parentId = permission.getParentBy();
        this.hidden = permission.getHidden();
        if (permission.getType() == 0){
            this.redirect = "noRedirect";  // 取消一级目录的'重新导向'功能
            this.alwaysShow = true;
        } else if(permission.getType() == 1){
            this.alwaysShow = false;
        } else {
            this.hidden = false;
            this.alwaysShow = false;
        }
        this.meta.setAffix(permission.getIsAffix());
        this.meta.setIcon(StringUtils.isEmpty(permission.getIcon())?"":permission.getIcon());
        this.meta.setTitle(permission.getName());
        this.meta.setNoCache(permission.getKeepAlive());
        this.meta.setRoles(StringUtils.isEmpty(permission.getAuthority()) ? new String[]{} : permission.getAuthority().split(","));
    }

    @Override
    public String getParentId() {
        return this.parentId;
    }

    @Override
    public void setParentId(String parentId) {
        this.parentId = parentId;
    }

    @Override
    public String getId() {
        return this.id;
    }

    @Override
    public void setId(String id) {
        this.id = id;
    }

    @Override
    public List<RouterTreeVo> getChildren() {
        return this.children;
    }

    @Override
    public void setChildren(List<RouterTreeVo> children) {
        this.children = children;
    }
}
